{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pymongo\n",
    "from libs.pascal_voc_io import PascalVocWriter\n",
    "import os.path\n",
    "\n",
    "try:\n",
    "    from PyQt5.QtGui import QImage\n",
    "except ImportError:\n",
    "    from PyQt4.QtGui import QImage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "db_name = 'labels_jyz'\n",
    "save_dir = 'gen/jyz'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "client = pymongo.MongoClient()\n",
    "db = client[db_name]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "img_list = list(db.labels_db.find())\n",
    "labels = set()\n",
    "for img_dic in img_list:\n",
    "    imagePath = img_dic['img_path']\n",
    "\n",
    "    imgFolderPath = os.path.dirname(imagePath)\n",
    "    imgFolderName = os.path.split(imgFolderPath)[-1]\n",
    "    imgFileName = os.path.basename(imagePath)\n",
    "    #imgFileNameWithoutExt = os.path.splitext(imgFileName)[0]\n",
    "    # Read from file path because self.imageData might be empty if saving to\n",
    "    # Pascal format\n",
    "    image = QImage()\n",
    "    image.load(imagePath)\n",
    "    imageShape = [image.height(), image.width(),1 if image.isGrayscale() else 3]\n",
    "\n",
    "    writer = PascalVocWriter(imgFolderName, imgFileName,imageShape, localImgPath=imagePath)\n",
    "\n",
    "    shapes = img_dic['bbs']\n",
    "    for shape in shapes:\n",
    "        difficult = 0\n",
    "        label = shape['label']\n",
    "        labels.add(label)\n",
    "\n",
    "        p0=shape['point_0']\n",
    "        p1=shape['point_1']\n",
    "        xmin = round(min(p0[0],p1[0]))\n",
    "        ymin = round(min(p0[1],p1[1]))\n",
    "        xmax = round(max(p0[0],p1[0]))\n",
    "        ymax = round(max(p0[1],p1[1]))\n",
    "        writer.addBndBox( xmin, ymin, xmax, ymax, label, difficult)\n",
    "    \n",
    "    dir_path = save_dir+\"/\"\n",
    "    if not os.path.exists(dir_path):\n",
    "        os.makedirs(dir_path) \n",
    "    writer.save(dir_path+os.path.splitext(imgFileName)[0]+'.xml')\n",
    "    for i in os.popen('cp '+imagePath + \" \"+dir_path+imgFileName).readlines():\n",
    "        print(i)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 生成obj.names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels = list(labels)\n",
    "labels.sort()\n",
    "\n",
    "names_file = open(save_dir+'/obj.names', 'w')\n",
    "for name in labels:\n",
    "    names_file.write(name + \"\\n\")\n",
    "names_file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
